package simorg.actions;

import java.util.Collection;

import org.dom4j.Document;
import org.dom4j.Element;

import simorg.controller.CourseController;
import simorg.domain.Course;
import simorg.domain.persistence.CourseTable;
import simorg.view.ModelView;
import simorg.view.Page;
import simorg.view.WebSessionContext;

public class CourseList extends Action {

	public static final String actionName = "list";
	public CourseList() {
		super(actionName);
		this.controllerName = CourseController.controllerName;
		
	}
	
	@Override
	public ModelView checkAccess(WebSessionContext wc) throws Exception {
		return this.checkAdminAccess(wc);
	}

	@Override
	public ModelView process(WebSessionContext wc) throws Exception {
		ModelView mv = this.checkAdminAccess(wc);
		if ( mv != null ) return mv;
		
		Document xmlDoc = this.getBaseXml(wc);
		
		Element courseList = xmlDoc.getRootElement().addElement( "courses" );
		Collection<Course> courses = CourseTable.findAll(wc.getDbConnection());
		for ( Course course : courses) {
			course.buildDomainXml(courseList, wc.getDbConnection());
		}
		
		return new ModelView(Page.Admin, xmlDoc);
	}

}
